/*
 * Copyright 2020-2025 the original author or authors.
 * You cannot use this file unless authorized by the author.
 */
package org.ipig.proc.unit;

import org.ipig.proc.core.Model;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * ModelUnit
 *
 * @author <a href="mailto:comchnts@163.com">chinats</a>
 * @version $$Id: ModelUnit.java 3 2020-01-21 09:36:33Z ts <comchnts@163.com> $$
 * @since 1.0
 */
@Data
@NoArgsConstructor
public class ModelUnit implements Model, Serializable {
    //业务，ns
    private String biz;
    //业务编码[8n]【维度】set
    private String dim;
    //【维度值】业务维度值[多个以下划线隔开] key
    private String key;
    //时间轴
    private Date time;
    //模型变更类型参考DbmsOperators.OpType
    private String type;
    //记录数量，新增操作为1，删除操作为-1，修改操作为0
    private Long count = 0L;
    //支持多度量指标，但目前同一个逻辑计算规则单元中只能处理一个度量指标，其它度量指标可做复杂的判断条件
    private Map<String, MetricsUnit> allMetrics = new HashMap<>(2);
    //所有字段KV
    private Map<String, String> allFields = new HashMap<>(8);
    //业务唯一ID
    private String id;
    //删除标识
    private String deleted;


    /**
     * 获取指定的度量指标
     *
     * @param name
     * @return
     */
    public MetricsUnit getMetrics(String name) {
        MetricsUnit metrics = this.allMetrics.get(name);
        return metrics;
    }

    /**
     * 获取指定的度量指标的值
     *
     * @param name
     * @return
     */
    public BigDecimal getMetricsValue(String name) {
        BigDecimal value = new BigDecimal(0);
        MetricsUnit procUnit = allMetrics.get(name);
        if (procUnit != null) {
            value = procUnit.getValue();
        }
        return value;
    }

    /**
     * 获取指定字段的值
     *
     * @param name
     * @return
     */
    public String getValue(String name) {
        return allFields.get(name);
    }

    /**
     * struct
     *
     * @param biz
     * @param dim
     * @param key
     * @param time
     */
    public ModelUnit(String biz, String dim, String key, Date time) {
        this.biz = biz;
        this.dim = dim;
        this.key = key;
        this.time = time;
    }
}
